package com.jlkf.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jlkf.dao.SysRoleMapper;
import com.jlkf.dao.SysUserMapper;
import com.jlkf.pojo.SysRole;
import com.jlkf.pojo.SysUser;
import com.jlkf.service.SysUserService;
import com.platform.server.response.response.pojo.ReturnData;
import com.platform.server.util.MD5Util;
import com.platform.server.util.RegexUtil;
import com.platform.server.util.StringTools;
@Service
public class SysUserServiceImpl implements SysUserService{

	@Autowired
	private SysUserMapper sysUserMapper;
	@Autowired
	private SysRoleMapper sysRoleMapper;
	
	
	@Override
	public Object getLogin(String json, HttpSession session) {
		SysUser su = StringTools.json2Obj(json, SysUser.class);
		if(su==null){
			return new ReturnData(201,"The parameter list is empty");
		}else if(StringTools.isEmpty(su.getTelephone())){
			return new ReturnData(201,"The phone number can't be empty !");
		}else if(StringTools.isEmpty(su.getPassword())){
			return new ReturnData(203,"The password cannot be empty!");
		}
		Map<String,Object> map = new HashMap<>();
		map.put("telephone", su.getTelephone());//电话号码
		try {
			SysUser su1 = sysUserMapper.getLogins(map);
			if(su1!=null){
//				String newPass = MD5Util.getMD5(su.getPassword());
				map.put("password", su.getPassword());
				SysUser su2 = sysUserMapper.getLogins(map);
				if(su2!=null){
					if(su2.getRoleId()!=null){
						SysRole sr=sysRoleMapper.selectByPrimaryKey(su2.getRoleId());
						session.setAttribute("srs", sr);
					}
					session.setAttribute("user", su1);
					return new ReturnData(su2,200,"success");
				}else{
					return new ReturnData(203,"Password mistake");
				}
			}else{
				return new ReturnData(202,"There is no account");
			}
		} catch (Exception e) {
		 e.printStackTrace();
		 return new ReturnData(500,"Wow, that's wrong");
		}
	}


	@Override
	public Object updateUser(String json,HttpServletRequest request) {
		SysUser su = StringTools.json2Obj(json, SysUser.class);
		 if(su==null){
			 return new ReturnData(201,"参数列表为空");
		 }else if(StringTools.isEmpty(su.getMail())){
			 return new ReturnData(202,"邮箱不能为空");
		 }else if(StringTools.isEmpty(su.getIdcard())){
			 return new ReturnData(202,"身份证号不能为空");
		 }else if(!StringTools.isEmpty(su.getMail())&&!RegexUtil.isEmail(su.getMail())){
			 return new ReturnData(206,"邮箱格式不正确");
		 }
		  HttpSession session = request.getSession();
		 try {
			
			 int num=sysUserMapper.updateByPrimaryKeySelective(su);
			 
			 if(num==0){
				 return new ReturnData(205,"更新失败");
			 }
			 session.removeAttribute("user");
			 SysUser s= sysUserMapper.selectByPrimaryKey(su.getId());
			 session.setAttribute("user", s);
			 return new ReturnData(200,"更新成功");
		} catch (Exception e) {
			 e.printStackTrace();
			 return new ReturnData(500,"Wow, that's wrong");
		}
	}


	@Override
	public Object getList(Integer pageNumber, Integer pageSize, String name, String account, Long roleId) {
		Map<String,Object> map = new HashMap<>();
	if(!StringTools.isEmpty(account)){
		map.put("account",account);
	}
	if(!StringTools.isEmpty(name)){
		map.put("name",name);
	}
	if(!RegexUtil.isNull(roleId)){
		map.put("roleId",roleId);
	}
	if(pageNumber==null){
		pageNumber=0;
	}
	if(pageSize==null){
		pageSize=15;
	}
	 try {
		 PageHelper.startPage(pageNumber, pageSize);
		  List<SysUser>  list= sysUserMapper.getList(map);
		  PageInfo<SysUser> info = new PageInfo<>(list);
		  return new ReturnData(list,(int)info.getPages(),(int)info.getTotal(),200,"sysuser_list");
	} catch (Exception e) {
		 e.printStackTrace();
		 return new ReturnData(500,"Wow, that's wrong");
	}
	}


	@Override
	public Object addOrUpdate(String json) {
		SysUser su = StringTools.json2Obj(json, SysUser.class);
		 if(su==null){
			 return new ReturnData(201,"The parameter list is empty.");
		 }/*else if(StringTools.isEmpty(su.getMail())){
			 return new ReturnData(202,"Mailbox cannot be empty.");
		 }else if(StringTools.isEmpty(su.getIdcard())){
			 return new ReturnData(202,"The id number cannot be empty.");
		 }*/else if(!StringTools.isEmpty(su.getMail())&&!RegexUtil.isEmail(su.getMail())){
			 return new ReturnData(206,"Incorrect email format.");
		 }
		 Map<String,Object> map = new HashMap<>();
		 map.put("telephone", su.getTelephone());
		 try {
			 
			if(su.getId()!=null){
				
				SysUser su1 =sysUserMapper.selectByPrimaryKey(su.getId());
				if(!su1.getTelephone().equals(su.getTelephone())){
					List<SysUser> list= sysUserMapper.getList(map);
					 if(list.size()>0){
						 return new ReturnData(300,"This number is already registered. Please enter a different number.");
					 }
				}
				su.setAccount(su.getTelephone());
				 int num=sysUserMapper.updateByPrimaryKeySelective(su);
				 if(num==0){
					 return new ReturnData(205,"更新失败");
				 }
			}else{
				List<SysUser> list= sysUserMapper.getList(map);
				 if(list.size()>0){
					 return new ReturnData(300,"This number is already registered. Please enter a different number.");
				 }
				su.setCreateTime(new Date());
				su.setAccount(su.getTelephone());
				int num=sysUserMapper.insertSelective(su);
				 if(num==0){
					 return new ReturnData(205,"更新失败");
				 }
			}
			 return new ReturnData(200,"更新成功");
		} catch (Exception e) {
			 e.printStackTrace();
			 return new ReturnData(500,"Wow, that's wrong");
		}
	}


	@Override
	public Object deleteRole(Long id) {
		try {
			int num=sysUserMapper.deleteByPrimaryKey(id);
			if(num==0){
				return new ReturnData(202,"删除失败");
			}
			return new ReturnData(200,"删除成功");
	} catch (Exception e) {
		e.printStackTrace();
		return new ReturnData(500,"Wow, that's wrong");
	}
	}

}
